package com.powernode.controller;


import com.powernode.common.Page;
import com.powernode.common.Result;
import com.powernode.constants.CoreConstant;
import com.powernode.domain.SysUser;
import com.powernode.enums.ResultEnums;
import com.powernode.enums.StateEnums;
import com.powernode.service.SysUserService;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sysUser")
public class SysUserController extends BaseController{
    @Resource
    private SysUserService sysUserService;

    /**
     * 分页查询用户
     * POST http://127.0.0.1:8080/sysUser/getByPage
     * 接口ID：60111900
     * 接口地址：https://www.apifox.cn/web/project/2244992/apis/api-60111900
     */
    @PostMapping("/getByPage")
    public Result<Page<SysUser>> getByPage(@RequestBody Page<SysUser> page){
      page=sysUserService.getByPage(page);
      return new Result<>(page);
    }
    /**
     * 添加角色
     * POST http://127.0.0.1:8080/sysUser/save
     * 接口ID：60111892
     * 接口地址：https://www.apifox.cn/web/project/2244992/apis/api-60111892
     */
    @PostMapping("/save")
    public Result<?> save(@RequestBody SysUser sysUser){
        SysUser user=sysUserService.queryUserByUserName(sysUser.getUserName());
        if (user!=null){
            if (user.getDelFlag().equals(StateEnums.DELETED.getCode())){
                return new Result<>(ResultEnums.USER_EXISTS,"有被删除的用户使用这个登录名，当前添加的用户不能使用");
            }
            return new Result<>(ResultEnums.USER_EXISTS);
        }
        //设置用户类型
        sysUser.setUserType(StateEnums.USER_TYPE_NORMAL.getCode());
        //设置头像
        sysUser.setAvatar(null);
        //设置默认密码
        sysUser.setPassword(DigestUtils.md5DigestAsHex(CoreConstant.DEFAULT_PASSWORD.getBytes()));
        //设置创建人
        sysUser.setCreateBy(getUsername());
        //设置创建时间
        sysUser.setCreateTime(new Date());
        sysUserService.insertSelective(sysUser);
        return new Result("添加成功");
    }

    /**
     * 根据ID查询角色
     * GET http://127.0.0.1:8080/sysUser/get/{roleId}
     * 接口ID：60111895
     * 接口地址：https://www.apifox.cn/web/project/2244992/apis/api-60111895
     */
    @GetMapping("/get/{roleId}")
    public Result<?> get(@PathVariable Long roleId){
        SysUser sysUser = sysUserService.selectByPrimaryKey(roleId);
        return new Result<>(sysUser);
    }

    /**
     * 修改角色
     * PUT http://127.0.0.1:8080/sysUser/update
     * 接口ID：60111893
     * 接口地址：https://www.apifox.cn/web/project/2244992/apis/api-60111893
     */
    @PutMapping("/update")
    public Result<?> update(@RequestBody SysUser sysUser){
        sysUser.setUpdateBy(getUsername());
        sysUser.setUpdateTime(new Date());
        sysUserService.updateByPrimaryKey(sysUser);
        return new Result<>("修改成功");
    }

    /**
     * 删除角色
     * DELETE http://127.0.0.1:8080/sysUser/delete/{roleId}
     * 接口ID：60111894
     * 接口地址：https://www.apifox.cn/web/project/2244992/apis/api-60111894
     */
    @DeleteMapping("/delete/{roleId}")
    public Result<?> delete(@PathVariable Long roleId){
        sysUserService.deleteByPrimaryKey(roleId);
        return new Result<>("删除成功");

    }

    /**
     * 重置用户密码
     * GET http://127.0.0.1:8080/sysUser/resetPwd/{userId}
     * 接口ID：43445103
     * 接口地址：https://www.apifox.cn/web/project/1739100/apis/api-43445103
     */
    @GetMapping("resetPwd/{userId}")
    public Result resetPwd(@PathVariable Long userId){
        SysUser sysUser = this.sysUserService.selectByPrimaryKey(userId);
        if(null==sysUser){
            return new Result(ResultEnums.USER_NOT_FOUND);
        }
        sysUser.setPassword(DigestUtils.md5DigestAsHex(CoreConstant.DEFAULT_PASSWORD.getBytes()));
        //修改
        this.sysUserService.updateByPrimaryKeySelective(sysUser);
        return new Result();
    }

}
